#!/bin/sh
#一行一行打印
usage()
{
	echo "Today's date is `date`"
	echo "The software's usage"
	echo "./IPv6G IPv6"
}

ipv6()
{
	echo "请选择策略，A或者B，首选为B"
	read scheme
	if [ "$scheme" = "B" ]; then
		B
	else
		A
	fi
}

B() 
{
echo "首选B策略"
echo "ASN+IPv4前缀+随机数"
asn=`expr 1024 "*" 8`
o=0
#asn_i=`echo "obase=16; $asn_j" | bc`
#	echo "$asn_i"

echo "请输入一个IPv4的前缀和IPv4前缀长度"
#IPv6前缀长度等于IPv4长度的2倍
read IPv4P IPv4L
IPv6L=`expr $IPv4L "*" 2`
randomL=`expr $IPv4L - 16`
echo "请输入一个 $randomL bit的随机值"
read random

while [ "$asn" -ge "$o" ]; do
s=$asn
i=`echo "obase=2; $asn" | bc`
iL=`echo $i|awk '{print length($i)}'`

        if [ "$iL" -lt "16" ]
        then
                z_number=`expr 16 - $iL`
                j=$i
                while [ "$z_number" -gt "$o" ]; do
                        j=0$j
                        z_number=`expr $z_number - 1`
                done
        fi

jL=`echo $j|awk '{print length($j)}'`

L1=`echo $j|awk '{print substr($j,1,4)}'`
L2=`echo $j|awk '{print substr($j,5,4)}'`
L3=`echo $j|awk '{print substr($j,9,4)}'`
L4=`echo $j|awk '{print substr($j,13,4)}'`

l1=`echo "obase=16; ibase=2; $L1"|bc`
l2=`echo "obase=16; ibase=2; $L2"|bc`
l3=`echo "obase=16; ibase=2; $L3"|bc`
l4=`echo "obase=16; ibase=2; $L4"|bc`

ASN=`echo "$l1$l2$l3$l4"`
asn=`expr $s - 1`

echo "$ASN:$IPv4P:$random/$IPv6L"

done
		
}

A()
{
	echo "A group"
}

ipv4()
{
	echo "IPv4 perfix"
#创建存储表的文件
	if [ -e yiyi ]; then
		echo "已经有yiyi"
		echo "是否删除原来的yiyi，yes或者no"
		read y_or_n
		case "$y_or_n" in
			y*) rm yiyi
			    echo "已经删除yiyi";;
			n*) echo "no";;
		esac
	else
		touch yiyi
	fi
#创建表	
	echo 请输入i的值
	read i
	j=0

	while [ "$j" -lt "$i" ]; do
		echo -e $j >> yiyi
		j=`expr $j + 1`
	done
}

case "$1" in
	IPv4) echo "IPv4 table"
	      ipv4;;
	IPv6) echo "IPv6 table"
	      ipv6;;
	* | h* | H*)    usage;;
esac

exit 0
